#include <stdio.h>
#include <string.h>

/* http://code.google.com/codejam/contest/351101/dashboard#s=p1 */

void reverse_words(char *string)
{
    char *str = string;
    char *first;
    char *last;
    while('\0' != *str) {
        // proceed to next non-blank
        while(*str == ' ')
            str++;
        first = str;

        // proceed to next blank
        while(*str != '\0' && *str != ' ')
            str++;
        last = str-1;
        while(first < last) {
            char c = *first;
            *first = *last;
            *last = c;
            first++;
            last--;
        }
    }

    char *end = str;
    end--;
    str = string;
    while(str < end) {
        char c = *str;
        *str = *end;
        *end = c;
        str++;
        end--;
    }
}

int main(int argc, char **argv)
{
    if(argc != 2) {
        fprintf(stderr, "usage: %s words\n", argv[0]);
        return -1;
    }

    reverse_words(argv[1]);

    printf("reversed words are: |%s|\n", argv[1]);
    return 0;
}
